<?php
class User extends AppModel {

	var $name = 'User';
	var $validate = array(
		'nev' => array('alphanumeric'),
		'pass' => array('alphanumeric')
	);
	//kössük össze a modellt acl-hez
	//var $actAs = array('Acl' => array('requester'));

	//The Associations below have been created with all possible keys, those that are not needed can be removed
	var $hasMany = array(
			'Bizonylat' => array('className' => 'Bizonylat',
								'foreignKey' => 'user_id',
								'dependent' => false,
								'conditions' => '',
								'fields' => '',
								'order' => '',
								'limit' => '',
								'offset' => '',
								'exclusive' => '',
								'finderQuery' => '',
								'counterQuery' => ''
			)
	);
	var $hasAndBelongsToMany = array(
			'Osztaly' => array('className' => 'Osztaly',
						'joinTable' => 'osztalyok_users',
						'foreignKey' => 'user_id',
						'associationForeignKey' => 'osztaly_id',
						'unique' => true,
						'conditions' => '',
						'fields' => '',
						'order' => '',
						'limit' => '',
						'offset' => '',
						'finderQuery' => '',
						'deleteQuery' => '',
						'insertQuery' => ''
			)
			);
	
/*
* Az adott user számára elérhető osztály id-k lekérése
* @param $userId a user id-je
* @return array hozzáférhető osztályok id-i
*/
	function getOsztalyId($userId, $ev){
		$sql = 'SELECT osztaly_id
					FROM osztalyok, osztalyok_users
					WHERE osztalyok_users.user_id = ' . $userId
					. ' AND osztalyok.id = osztalyok_users.osztaly_id
					AND osztalyok.ev IN (0, ' . $ev . ')';
		
		$eredmeny = $this->query($sql);
		$oIds = array();
		foreach($eredmeny as $o){
			$oIds[] = $o['osztalyok_users']['osztaly_id'];
		}
		
		/*//amíg nincsenek beállítva a hozzáférések addig engedjünk mindekit mindenhez
		$sql = 'SELECT id FROM osztalyok';
		$eredmeny = $this->query($sql);
		$oIds = array();
		foreach($eredmeny as $o){
			$oIds[] = $o['osztalyok']['id'];
		}*/

		return($oIds);
	}

}
?>